<template>
    <van-nav-bar title="退货商品" left-text="" left-arrow @click-left="goBack" />
    <section class="aui-flexView">
        <!-- 订单详细 -->
        <section class="aui-scrollView">
            <!-- 订单状态 -->
            <div class="aui-pay-change">
                <div class="aui-flex">
                    <div class="aui-flex-box">
                        <h3>{{ detail.status_text }}</h3>
                    </div>
                    <div class="aui-pay-zfb">
                        <img src="/assets/images/icon-zfb.png" alt="" />
                    </div>
                </div>
            </div>

            <!-- 收货地址 -->
            <div class="aui-flex">
                <div style="font-size: 16px">退货地址：</div>
                <div class="aui-flex-box">
                    <h3>
                        {{ detail.refund.company }} <em>{{ detail.refund.mobile }}</em>
                    </h3>
                    <p>{{ detail.refund.address }}</p>
                </div>
            </div>

            <!-- 退货信息 -->
            <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <!-- 寄件地址列表 -->
                    <van-field v-model="region_text" is-link readonly name="picker" label="寄件地址列表"
                        placeholder="点击寄件地址列表" @click="showRegionPicker = true"
                        :rules="[{ required: true, message: '请选择寄件地址' }]" />
                    <van-popup v-model:show="showRegionPicker" destroy-on-close position="bottom">
                        <van-picker :columns="Regioncolumns" @confirm="onRegionConfirm"
                            @cancel="showRegionPicker = false" />
                    </van-popup>

                    <!-- 物流公司列表 -->
                    <van-field v-model="express_text" is-link readonly name="picker" label="物流公司" placeholder="选择物流公司"
                        @click="showExpressPicker = true" :rules="[{ required: true, message: '请选择物流公司' }]" />
                    <van-popup v-model:show="showExpressPicker" destroy-on-close position="bottom">
                        <van-picker :columns="Expresscolumns" @confirm="onExpressConfirm" @cancel="showExpressPicker = false" />
                    </van-popup>

                    <van-field v-model="expresscode" type="text" name="expresscode" label="快递单号" placeholder="输入快递单号"
                        :rules="[{ required: true, message: '请填写快递单号' }]" />
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button round block type="primary" native-type="submit">
                        提交
                    </van-button>
                </div>
            </van-form>
        </section>
    </section>
</template>
<script setup>
const { proxy } = getCurrentInstance()
const goBack = () => { proxy.$router.push({
    path:'/order/index',
    query:{
        status:-1
    }})
}
const LoginUser = reactive(proxy.$cookies.get('LoginUser') ? proxy.$cookies.get('LoginUser') : {})
const orderid = proxy.$route.query.orderid ? proxy.$route.query.orderid : 0;
const detail = ref({
    business: {},
    address: {},
    refund: {}
})
// 寄件地址
const region_text = ref('')
const showRegionPicker = ref(false)
const region = ref('')
const Regioncolumns = ref([])

// 物流公司
const express_text = ref('')
const showExpressPicker = ref(false)
const Expresscolumns = ref([])
const expressid = ref('')

// 快递单号
const expresscode = ref('')

onBeforeMount(() => {
    getOrderInfoData();
    getAddressData()
    getExpressData()
})
const getOrderInfoData = async () => {
    let result = await Api.orderInfo({
        busid: LoginUser.id,
        orderid: orderid
    });

    if (result.code == 1) {
        detail.value = result.data
    }
}

const getAddressData = async() =>{
    let result = await Api.addressIndex({busid: LoginUser.id})
    if(result.code == 1)
    {
        result.data.forEach((item) =>{
            Regioncolumns.value.push({
                text: `${item.consignee} - ${item.mobile} - ${item.region_text}${item.address}`,
                value: item.id
            })
        })
    }
}

const getExpressData = async() =>{
    let result = await Api.orderLogistics({busid: LoginUser.id})
    if(result.code == 1)
    {
        Expresscolumns.value = result.data
    }
}

const onRegionConfirm = ({ selectedValues, selectedOptions }) => {
    region_text.value = selectedOptions[0]?.text;
    region.value = selectedOptions[0]?.value;
    showRegionPicker.value = false;
}

const onExpressConfirm = ({ selectedValues, selectedOptions }) => {
    express_text.value = selectedOptions[0]?.text;
    expressid.value = selectedOptions[0]?.value;
    showExpressPicker.value = false;
}

const onSubmit = async() =>{
    let data = {
        busid: LoginUser.id,
        orderid: orderid,
        addressid: region.value,
        expressid: expressid.value,
        expresscode: expresscode.value
    }

    let result = await Api.orderRefundgoods(data)
    if (result.code == 1) 
    {
        success({
            message: result.msg,
            onClose: () => {
                goBack()
            }
        });
    } 
    else 
    {
        error(result.msg);
    }
}

</script>
<style>
@import url('/assets/css/info.css');
</style>